package com.my.yyr.controller.safty;

import com.my.yyr.base.Constants;
import com.my.yyr.base.CurrUser;
import com.my.yyr.base.Result;
import com.my.yyr.dto.UserDto;
import com.my.yyr.exception.BusinessException;
import com.my.yyr.exception.SysException;
import com.my.yyr.service.safty.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/safty/login")
//@SessionAttributes({Constants.SESS_ATTR_CURRUSER})//说明model中的哪些属性需要放到Session中
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("")
    public Result execLogin(@RequestBody UserDto userDto , HttpSession session){

        try {
            CurrUser currUser = loginService.checkLogin(userDto);
            System.out.println("===================>"+currUser.getUserName());
            session.setAttribute(Constants.SESS_ATTR_CURRUSER,currUser);
            return Result.success("登录成功！");
        }catch(BusinessException e) {
            return Result.fail(e.getMessage());
        }catch(SysException e){
            return Result.fail("升级中，请稍后再试。");
        }
    }

    //@GetMapping("")
    public Result test(){
        return Result.success("测试成功了！");
    }
}
